//全局的单一事件管理器
let events = [];
module.exports = {
    on(eventName, fun, target) {
        events.push({ eventName: eventName, fun: fun, target: target });
    },
    off(eventName, fun, target) {
        events = events.filter(item => { return item.eventName != eventName || item.fun != fun || item.target != target; });
    },
    emit(eventName, ...args) {
        let currentEvents = events.filter(item => { return item.eventName == eventName });
        events = events.filter(item => { return item.eventName != eventName || (item.eventName == eventName && !item.isOnce) });
        currentEvents.map(item => {
            item.fun.apply(item.target, args);
        });
    },
    removeAllListeners(eventName) {
        if(typeof(eventName) == 'object'){
            events = events.filter(item => { return item.target != eventName });
        } else {
            events = events.filter(item => { return item.eventName != eventName });
        }
    },
    once(eventName, fun, target) {
        events.push({ eventName: eventName, fun: fun, target: target, isOnce: true });
    }
};